package com.zyguo.sdk.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

public class BaseDAO {
	private WriteClient writeClient;
	private ReadClient readClient;
	private Logger LOG = Logger.getLogger( this.getClass() );
	private static final int UPDATE_MAX_COUNT = 500;

	protected Object queryForObject(String statementName) {
		return this.getSqlReadTemplate().queryForObject(statementName);
	}

	protected Object queryForObject(String statementName, Object parameterObject) {
		return this.getSqlReadTemplate().queryForObject(statementName, parameterObject);
	}

	public List queryForList(String statementName) {
		return this.getSqlReadTemplate().queryForList(statementName);
	}

	public List queryForList(String statementName, Object parameterObject) {
		return this.getSqlReadTemplate().queryForList(statementName, parameterObject);
	}

	protected Object insert(String statementName) {
		return this.getSqlWriteTemplate().insert(statementName);
	}

	protected Object insert(String statementName, Object parameterObject) {
		return this.getSqlWriteTemplate().insert(statementName, parameterObject);
	}

	protected int delete(String statementName) {
		return this.getSqlWriteTemplate().delete(statementName);
	}

	protected int delete(String statementName, Object parameterObject) {
		return this.getSqlWriteTemplate().delete(statementName, parameterObject);
	}

	protected int update(String statementName) {
		int updateNum = this.getSqlWriteTemplate().update(statementName);
		if (updateNum > UPDATE_MAX_COUNT) {
			LOG.warn("update mysql data over " + UPDATE_MAX_COUNT + ":" + statementName);
		}
		return updateNum;
	}

	protected int update(String statementName, Object parameterObject) {
		int updateNum = this.getSqlWriteTemplate().update(statementName, parameterObject);
		if (updateNum > UPDATE_MAX_COUNT) {
			LOG.warn("update mysql data over " + UPDATE_MAX_COUNT + ":" + statementName);
		}
		return updateNum;
	}

	private SqlMapClientTemplate getSqlReadTemplate() {
		return this.readClient.getSqlMapClientTemplate();
	}

	private SqlMapClientTemplate getSqlWriteTemplate() {
		return this.writeClient.getSqlMapClientTemplate();
	}

	public void setWriteClient(WriteClient writeClient) {
		this.writeClient = writeClient;
	}

	public void setReadClient(ReadClient readClient) {
		this.readClient = readClient;
	}

}
